package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.efounder.chat.utils.CommonThreadPoolUtils;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.util.AppContext;
import com.efounder.util.ToastUtil;
import com.efounder.utils.ImageSpliceUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.util.TCRequestUtil;
import com.qmuiteam.qmui.util.QMUIDrawableHelper;
import com.utilcode.util.ImageUtils;
import com.utilcode.util.LogUtils;
import com.utilcode.util.ReflectUtils;
import com.utilcode.util.ToastUtils;

import java.io.File;
import java.util.HashMap;


/**
 * 邀请好友海报
 * Created by yqs on 2018/11/23
 */

public class InviteFriendPosterActivity extends TCBaseActivity implements View.OnClickListener {
    public static final String TAG = "InviteFriendPosterActivity";

    private ScrollView scrollview;
    private LinearLayout llContent;
    private TextView tvTopTitle;
    private ImageView ivBg;
    private TextView tvTextLeft;
    private ImageView ivJifen;
    private TextView tvTextRight;
    private ImageView ivQrcode;
    private TextView tvBottomTips;
    private LinearLayout llBottom;
    private LinearLayout llSave;
    private LinearLayout llShare;

    private ReflectUtils reflectUtils;

    public static void start(Context context) {
        Intent starter = new Intent(context, InviteFriendPosterActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.assets_activity_invite_friend_poster);
        initView();
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            reflectUtils.method("release", this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        scrollview = (ScrollView) findViewById(R.id.scrollview);
        llContent = (LinearLayout) findViewById(R.id.ll_content);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        ivBg = (ImageView) findViewById(R.id.iv_bg);
        tvTextLeft = (TextView) findViewById(R.id.tv_text_left);
        ivJifen = (ImageView) findViewById(R.id.iv_jifen);
        tvTextRight = (TextView) findViewById(R.id.tv_text_right);
        ivQrcode = (ImageView) findViewById(R.id.iv_qrcode);
        tvBottomTips = (TextView) findViewById(R.id.tv_bottom_tips);
        llBottom = (LinearLayout) findViewById(R.id.ll_bottom);
        llSave = (LinearLayout) findViewById(R.id.ll_save);
        llShare = (LinearLayout) findViewById(R.id.ll_share);
        llSave.setOnClickListener(this);
        llShare.setOnClickListener(this);
    }

    private void initData() {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_inviate_title));

        //初始化友盟分享
        try {
            reflectUtils = ReflectUtils.reflect(ResStringUtil.getString(R.string.manager_share_screen_shot_class));
            reflectUtils = reflectUtils.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        requestData();


    }

    private void requestData() {
        showLoading(R.string.common_text_please_wait);
        final HashMap<String, String> map = new HashMap<>();
        map.put("userId", EnvironmentVariable.getUserName());

        TCRequestUtil.getCommonRequest(TAG, "aridrop/airportDetails", map, new TCRequestUtil.TCRequestCallback() {
            @Override
            public void onSuccess(String response) {
                dismissLoading();
                try {

                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtils.showShort(R.string.common_text_http_request_fail);
                }

            }

            @Override
            public void onFail(String error) {
                dismissLoading();
                ToastUtils.showShort(R.string.common_text_http_request_fail);
            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_save) {
            createAndSaveScreenShot(false);
        } else if (id == R.id.ll_share) {
            createAndSaveScreenShot(true);
        }
    }

    private void createAndSaveScreenShot(final boolean isShare) {
        showLoading(R.string.common_text_please_wait);

        CommonThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = QMUIDrawableHelper.createBitmapFromView(llContent);
                final String fielPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
                        + File.separator
                        + System.currentTimeMillis() + ".png";
                ImageUtils.save(bitmap, fielPath, Bitmap.CompressFormat.PNG, true);
                //通知图库刷新
                ImageSpliceUtil.galleryAddPic(fielPath);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!InviteFriendPosterActivity.this.isDestroyed() && !InviteFriendPosterActivity.this.isFinishing()) {
                            dismissLoading();
                            if (isShare) {
                                try {
                                    reflectUtils.method("share", InviteFriendPosterActivity.this, fielPath,
                                            fielPath, R.drawable.ic_launcher, "", null);
                                } catch (Exception e) {
                                    LogUtils.d("没有找到分享的类，分享原图");
                                    new File(fielPath).delete();
                                }

                            } else {
                                ToastUtil.showToast(AppContext.getInstance(), R.string.open_planet_packet_poster_saveed);

                            }

                        }

                    }
                });
            }
        });
    }
}